<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图书id" prop="bookId">
        <el-input
          v-model="queryParams.bookId"
          placeholder="请输入图书id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借书id" prop="borrowId">
        <el-input
          v-model="queryParams.borrowId"
          placeholder="请输入借书id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="还书id" prop="returnId">-->
<!--        <el-input-->
<!--          v-model="queryParams.returnId"-->
<!--          placeholder="请输入还书id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="信用度偏移量" prop="creditOffset">-->
<!--        <el-input-->
<!--          v-model="queryParams.creditOffset"-->
<!--          placeholder="请输入信用度偏移量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="上链状态" prop="creditTobc">
        <el-select v-model="queryParams.creditTobc" placeholder="请选择上链状态" clearable>
          <el-option
            v-for="dict in dict.type.library_tobc"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="备用字段1" prop="tag1">-->
<!--        <el-input-->
<!--          v-model="queryParams.tag1"-->
<!--          placeholder="请输入备用字段1"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用字段2" prop="tag2">-->
<!--        <el-input-->
<!--          v-model="queryParams.tag2"-->
<!--          placeholder="请输入备用字段2"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['librarysystem:librarycredit:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['librarysystem:librarycredit:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['librarysystem:librarycredit:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['librarysystem:librarycredit:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="librarycreditList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="信用度信息id" align="center" prop="creditId" />-->
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="图书id" align="center" prop="bookId" />
      <el-table-column label="借书id" align="center" prop="borrowId" />
<!--      <el-table-column label="还书id" align="center" prop="returnId" />-->
      <el-table-column label="信用度偏移量" align="center" prop="creditOffset" />
      <el-table-column label="上链状态" align="center" prop="creditTobc">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.library_tobc" :value="scope.row.creditTobc"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="备用字段1" align="center" prop="tag1" />-->
<!--      <el-table-column label="备用字段2" align="center" prop="tag2" />-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['librarysystem:librarycredit:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['librarysystem:librarycredit:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改信用度信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="图书id" prop="bookId">
          <el-input v-model="form.bookId" placeholder="请输入图书id" />
        </el-form-item>
        <el-form-item label="借书id" prop="borrowId">
          <el-input v-model="form.borrowId" placeholder="请输入借书id" />
        </el-form-item>
        <el-form-item label="还书id" prop="returnId">
          <el-input v-model="form.returnId" placeholder="请输入还书id" />
        </el-form-item>
        <el-form-item label="信用度偏移量" prop="creditOffset">
          <el-input v-model="form.creditOffset" placeholder="请输入信用度偏移量" />
        </el-form-item>
        <el-form-item label="上链状态" prop="creditTobc">
          <el-input v-model="form.creditTobc" placeholder="请输入上链状态" />
        </el-form-item>
        <el-form-item label="备用字段1" prop="tag1">
          <el-input v-model="form.tag1" placeholder="请输入备用字段1" />
        </el-form-item>
        <el-form-item label="备用字段2" prop="tag2">
          <el-input v-model="form.tag2" placeholder="请输入备用字段2" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLibrarycredit, getLibrarycredit, delLibrarycredit, addLibrarycredit, updateLibrarycredit } from "@/api/librarysystem/librarycredit";

export default {
  name: "Librarycredit",
  dicts: ['library_tobc'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信用度信息管理表格数据
      librarycreditList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        bookId: null,
        borrowId: null,
        returnId: null,
        creditOffset: null,
        creditTobc: null,
        tag1: null,
        tag2: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        bookId: [
          { required: true, message: "图书id不能为空", trigger: "blur" }
        ],
        borrowId: [
          { required: true, message: "借书id不能为空", trigger: "blur" }
        ],
        returnId: [
          { required: true, message: "还书id不能为空", trigger: "blur" }
        ],
        creditOffset: [
          { required: true, message: "信用度偏移量不能为空", trigger: "blur" }
        ],
        creditTobc: [
          { required: true, message: "上链状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询信用度信息管理列表 */
    getList() {
      this.loading = true;
      listLibrarycredit(this.queryParams).then(response => {
        this.librarycreditList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        creditId: null,
        userId: null,
        bookId: null,
        borrowId: null,
        returnId: null,
        creditOffset: null,
        creditTobc: null,
        tag1: null,
        tag2: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.creditId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加信用度信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const creditId = row.creditId || this.ids
      getLibrarycredit(creditId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改信用度信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.creditId != null) {
            updateLibrarycredit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLibrarycredit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const creditIds = row.creditId || this.ids;
      this.$modal.confirm('是否确认删除信用度信息管理编号为"' + creditIds + '"的数据项？').then(function() {
        return delLibrarycredit(creditIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('librarysystem/librarycredit/export', {
        ...this.queryParams
      }, `librarycredit_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
